package view.component.window
{
import core.Language;

import framework.view.components.button.ButtonProxy;
import framework.view.components.container.RepeaterProxy;

import mapengine.interfaces.IMapBuilding;

import values.building.FinalBuildFriendCostPositionValue;
import values.item.IItemValue;

import view.component.render.SpecialFinalBuildItemRender;

/**
 * SpecialFinalBuildUIProxy
 * @author jliang
 *
 */
public class SpecialFinalBuildUIProxy extends WindowProxy
{
	//==========================================================================
	//  Constructor
	//==========================================================================
	public function SpecialFinalBuildUIProxy()
	{
		super();
	}
	//==========================================================================
	//  Properties
	//==========================================================================
	protected var _data:IMapBuilding;
	
	public function get data():IMapBuilding
	{
		return _data;
	}
	
	public function set data(value:IMapBuilding):void
	{
		_data = value;
		
		var positions:Vector.<FinalBuildFriendCostPositionValue> = data.buildingItemValue.finalBuildFriend;
		
		var resources:Vector.<IItemValue> = data.buildingItemValue.buildingTemplate.finalBuildResourceCost.costValueList;
		
		var maxCount:int = list.itemProxys.length;
		
		var costItems:Vector.<Object> = new Vector.<Object>();
		
		for (var i:int = 0; i < positions.length; i++)
		{
			costItems.push(positions[i]);
		}
		
		for (var j:int = 0; j < resources.length; j++)
		{
			costItems.push(resources[j]);
		}
		
		list.data = costItems;
		
		check();
	}
	
	public function get list():RepeaterProxy
	{
		return proxys["list"];
	}
	
	public function get okBtn():ButtonProxy
	{
		return proxys["okBtn"];
	}
	//==========================================================================
	//  Overridden methods: ComponentProxy
	//==========================================================================
	override protected function get proxyMapping():Array
	{
		return [ 
			{ skin:"list", classRef: RepeaterProxy, id:"list", renderProxyRef:SpecialFinalBuildItemRender },
			{ skin:"okBtn", classRef: ButtonProxy, id:"okBtn", text:Language.getText("@finish_build_button_label"), onClick: onClickOkHandler }
		].concat(super.proxyMapping);
	}
	//==========================================================================
	//  Protected methods
	//==========================================================================
	protected function check():void
	{
		var isMeetRequired:Boolean = true;
		var render:SpecialFinalBuildItemRender;
		
		for each (render in list.itemProxys)
		{
			if (render.data)
			{
				if (!render.isMeetRequired)
				{
					isMeetRequired = false;
					break;
				}
			}
		}
		
		okBtn.enabled = isMeetRequired;
	}
	
	protected function finishBuild():void
	{
		data.doAction();
		close();
	}
	//==========================================================================
	//  Event handlers
	//==========================================================================
	protected function onClickOkHandler():void
	{
		finishBuild();
	}
}
}




